package com.vincent.service;

import com.vincent.common.enums.ResultMapKey;
import com.vincent.dao.MenuPermissionDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by vincent on 15-1-6.
 */
@Service("menuPermissionService")
public class MenuPermissionService {

    @Resource
    private MenuPermissionDao menuPermissionDao;


    public Map<String,Object> getPermissionIdsByMenuId(Integer menuId){
        Map<String,Object> map=new HashMap<String, Object>();
        List<Integer> perIdsByMenuId = menuPermissionDao.getPerIdsByMenuId(menuId);
        map.put(ResultMapKey.SUCCESS.getCode(),perIdsByMenuId);
        return map;
    }

    public Map<String,Object> grantMenuPermissions(Integer menuId,Integer []perIds){
        Map<String,Object> map=new HashMap<String, Object>();
        menuPermissionDao.deleteByMenuIds(new Integer[]{menuId});
        if(perIds.length>1||(perIds.length==1&&perIds[0]!=0)){
            menuPermissionDao.saveMenuPermissions(menuId,perIds);
        }
        map.put(ResultMapKey.SUCCESS.getCode(),"菜单授权成功");
        return map;
    }
}
